<?php

/**
 * Proveedor form base class.
 *
 * @method Proveedor getObject() Returns the current form's model object
 *
 * @package    aeropostal
 * @subpackage form
 * @author     Your name here
 */
abstract class BaseProveedorForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'rif'             => new sfWidgetFormInputHidden(),
      'nombre'          => new sfWidgetFormInputText(),
      'tlf'             => new sfWidgetFormInputText(),
      'direccion'       => new sfWidgetFormInputText(),
      'tipo'            => new sfWidgetFormInputText(),
      'uMedida'         => new sfWidgetFormPropelChoice(array('model' => 'Unidmedida', 'add_empty' => true)),
      'moneda'          => new sfWidgetFormPropelChoice(array('model' => 'Moneda', 'add_empty' => true)),
      'contacto'        => new sfWidgetFormInputText(),
      'tlfContacto'     => new sfWidgetFormInputText(),
      'logotipo'        => new sfWidgetFormInputText(),
      'preciocomb_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Estacion')),
    ));

    $this->setValidators(array(
      'rif'             => new sfValidatorChoice(array('choices' => array($this->getObject()->getRif()), 'empty_value' => $this->getObject()->getRif(), 'required' => false)),
      'nombre'          => new sfValidatorString(array('max_length' => 255)),
      'tlf'             => new sfValidatorString(array('max_length' => 255)),
      'direccion'       => new sfValidatorString(array('max_length' => 255)),
      'tipo'            => new sfValidatorString(array('max_length' => 255)),
      'uMedida'         => new sfValidatorPropelChoice(array('model' => 'Unidmedida', 'column' => 'simbolo', 'required' => false)),
      'moneda'          => new sfValidatorPropelChoice(array('model' => 'Moneda', 'column' => 'id', 'required' => false)),
      'contacto'        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'tlfContacto'     => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'logotipo'        => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'preciocomb_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Estacion', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('proveedor[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Proveedor';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['preciocomb_list']))
    {
      $values = array();
      foreach ($this->object->getPreciocombs() as $obj)
      {
        $values[] = $obj->getEstacion();
      }

      $this->setDefault('preciocomb_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->savePreciocombList($con);
  }

  public function savePreciocombList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['preciocomb_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(PreciocombPeer::PROVEEDOR, $this->object->getPrimaryKey());
    PreciocombPeer::doDelete($c, $con);

    $values = $this->getValue('preciocomb_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Preciocomb();
        $obj->setProveedor($this->object->getPrimaryKey());
        $obj->setEstacion($value);
        $obj->save();
      }
    }
  }

}
